package com.itheima.web.controller.system;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.Impl.DeptServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/dept")
public class DeptServlet extends HttpServlet {
    DeptService deptService=new DeptServiceImpl();
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的编码    在filter 过滤器中写
//        req.setCharacterEncoding("UTF-8");
//        resp.setContentType("text/html;charset=UTF-8");

        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            findAll(req, resp);
        } else if ("toAdd".equals(operation)) {
            List<Dept> deptList = deptService.findAll();
            req.setAttribute("deptList",deptList);
            req.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(req,resp);
        } else if ("save".equals(operation)) {
            //   Map<String, String[]> parameterMap = req.getParameterMap();
            Dept dept = BeanUtil.fillBean(req, Dept.class,"yyyy-MM-dd");
            deptService.save(dept);
            resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
        } else if ("delete".equals(operation)) {
            String id = req.getParameter("id");
            deptService.delete(id);
            resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
        } else if ("toEdit".equals(operation)) {
            String id = req.getParameter("id");
            List<Dept> deptList = deptService.findAll();
            Dept dept = deptService.findById(id);
            req.setAttribute("deptList",deptList);
            req.setAttribute("dept",dept);
            req.getRequestDispatcher(req.getContextPath()+"/WEB-INF/pages/system/dept/update.jsp").forward(req,resp);
        } else if ("edit".equals(operation)) {
            Dept dept = BeanUtil.fillBean(req, Dept.class,"yyyy-MM-dd");
            deptService.update(dept);
            resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
        }


    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page=1;
        int size=5;
        String page1 = req.getParameter("page");
        String size1 = req.getParameter("size");
        if (StringUtils.isNotBlank(page1)) {
            page = Integer.parseInt(page1);
        }
        if (StringUtils.isNotBlank(size1)) {
            size=Integer.parseInt(size1);
        }
        List<Dept> all = deptService.findAll(page, size);
        PageInfo<Dept> deptPageInfo = new PageInfo<>(all);
        req.setAttribute("page",deptPageInfo);
        //转发 web-inf 前要加"/"
        req.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(req,resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
